﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MP.Core.Library
{
    public class FDataTable
    {
        public static List<string> ConvertColumn2Array(DataTable tbl, string colName)
        {
            List<string> cols = new List<string>();
            tbl.DefaultView.Sort = colName;
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                if (!cols.Contains(tbl.DefaultView[i].Row[colName].ToString()))
                    cols.Add(tbl.DefaultView[i].Row[colName].ToString());
            }
            return cols;
        }

        public static DataTable QueryTable(DataTable table, string sort, params string[] query)
        {
            DataView view = new DataView(table);
            for (int i = 0; i < query.Length; i++)
            {
                view = new DataView(view.ToTable(), query[i], sort, DataViewRowState.CurrentRows);
            }
            return view.ToTable();
        }

        public static DataTable QueryTable(DataTable table, string sort, List<string> query)
        {
            DataView view = new DataView(table);
            for (int i = 0; i < query.Count; i++)
            {
                view = new DataView(view.ToTable(), query[i], sort, DataViewRowState.CurrentRows);
            }
            return view.ToTable();
        }
    }
}
